/**
* Modified FileField supporting passing file URI instead of actual file
* Author: Oleg Sukhoroslov
*/

(function() {

    var Event = YAHOO.util.Event;

    /**
     * Create a file input
     * @class inputEx.FileURLField
     * @extends inputEx.Field
     * @constructor
     * @param {Object} options Added options:
     * <ul>
     * </ul>
     */
    inputEx.FileURLField = function(options) {
        inputEx.FileURLField.superclass.constructor.call(this,options);
    };
    YAHOO.lang.extend(inputEx.FileURLField, inputEx.Field, {
	
    /**
     * Adds size and accept options
     * @param {Object} options Options object as passed to the constructor
     */
    setOptions: function(options) {
        inputEx.FileURLField.superclass.setOptions.call(this, options);
        this.options.size = options.size;
        this.options.accept = options.accept;
    },
	
    /**
     * Render an 'INPUT' DOM node
     */
    renderComponent: function() {
        // Attributes of the input field
        var attributes = {};
        attributes.id = this.divEl.id?this.divEl.id+'-field':YAHOO.util.Dom.generateId();
        attributes.type = "file";
        if (this.options.name) attributes.name = this.options.name;
        if (this.options.size) attributes.size = this.options.size;
        if (this.options.accept) attributes.accept = this.options.accept;

        // File and URI input fields
        this.fileEl = inputEx.cn('input', attributes);
        attributes.type = 'text';
        if (this.options.value) attributes.value = this.options.value;
        this.urlEl = inputEx.cn('input', attributes);

        // Wrapper element
        this.wrapEl = inputEx.cn('div', {className: 'inputEx-FileURLField-wrapper'});
        if (this.options.value) {
            this.wrapEl.appendChild(this.urlEl);
        } else {
            this.wrapEl.appendChild(this.fileEl);
        }
        
        // Switch button
        this.switchEl = inputEx.cn('input', {type: 'button'});
        if (this.options.value) {
            this.switchEl.value = 'Switch to File';
        } else {
            this.switchEl.value = 'Switch to URL';
        }
        this.wrapEl.appendChild(this.switchEl);
        Event.addListener(this.switchEl,'click',function(e){
            if (this.switchEl.value === 'Switch to URL') {
                this.wrapEl.removeChild(this.fileEl);
                this.wrapEl.insertBefore(this.urlEl, this.switchEl);
                this.switchEl.value = 'Switch to File';
            } else {
                this.wrapEl.removeChild(this.urlEl);
                this.wrapEl.insertBefore(this.fileEl, this.switchEl);
                this.switchEl.value = 'Switch to URL';
            }
        },this,true);

        // Append wrapper to the main element
        this.fieldContainer.appendChild(this.wrapEl);
    },
    
    /**
     * Register the change, focus and blur events
     */
    initEvents: function() {
        Event.addListener(this.urlEl, "change", this.onChange, this, true);
        if (YAHOO.env.ua.ie){ // refer to inputEx-95
            var field = this.urlEl;
            new YAHOO.util.KeyListener(this.urlEl, {keys:[13]}, {fn:function(){
                field.blur();
                field.focus();
            }}).enable();
        }
        Event.addFocusListener(this.urlEl, this.onFocus, this, true);
        Event.addBlurListener(this.urlEl, this.onBlur, this, true);
    },

    /**
     * Validation of the field
     * @return {Boolean} field validation status (true/false)
     */
    validate: function() {
        if (document.getElementById(this.options.id+'-field') != null) {
            var value = document.getElementById(this.options.id+'-field').value;
            if (value === '') {
                if (this.options.required)
                    return false;
            } else {
                // validate URI
                if (document.getElementById(this.options.id+'-field').type === 'text') {
                    return value.match(inputEx.regexps.url);
                }
            }
        }
        return true;
    }

});

// Register this class as "file-url" type
inputEx.registerType("file-url", inputEx.FileURLField);

})();
